package com.caiyi.financial.nirvana.sms.mapper;

import com.alibaba.druid.util.StringUtils;
import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.querybean.QueryKeywordBean;
import com.caiyi.financial.nirvana.sms.entity.SmsKeyword;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsKeywordMapper extends BaseMapper<SmsKeyword> {

    @SelectProvider(type = KeywordSqlProvider.class, method = "selectKeywordListByParams")
    List<SmsKeyword> selectKeywordListByParams(QueryKeywordBean bean);

    class KeywordSqlProvider {
        public String selectKeywordListByParams(QueryKeywordBean bean) {
            return new SQL() {
                {
                    SELECT("id,type,content,is_regexp,create_time,update_time ");
                    FROM("sms_keyword");
                    if (!StringUtils.isEmpty(bean.getContent())) {
                        WHERE("content LIKE concat('%',#{content},'%')");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    if (bean.getType()!= null) {
                        WHERE("type=#{type}");
                    }
                    if (bean.getIsRegexp()!= null) {
                        WHERE("is_regexp=#{isRegexp}");
                    }
                    ORDER_BY("id desc");

                }
            }.toString();
        }

    }
}
